package bluetooth;

import lejos.nxt.*;
import lejos.nxt.comm.*;

import java.io.*;

/**
 * Based on the source Lawrie Griffiths
 */
public class BTReceive {

	private InputStream dis;
	private OutputStream dos;
	private BTConnection btc;

	public void connect() {
		String connected = "Connected";
		String waiting = "Waiting...";

		LCD.drawString(waiting, 0, 0);
		LCD.refresh();

		btc = Bluetooth.waitForConnection();

		LCD.clear();
		LCD.drawString(connected, 0, 0);
		LCD.refresh();

		dis = btc.openDataInputStream();
		dos = btc.openDataOutputStream();

	}

	public byte[] read() throws IOException {
		byte[] values = new byte[3];
		dis.read(values);
		return values;
	}

	public void write(byte[] values) throws IOException {
		dos.write(values);
		dos.flush();
	}

	public void close() throws IOException, InterruptedException {
		String closing = "Closing...";
		dis.close();
		dos.close();
		Thread.sleep(100); // wait for data to drain
		LCD.clear();
		LCD.drawString(closing, 0, 0);
		LCD.refresh();
		btc.close();
		LCD.clear();
	}
}
